﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CUiJVLEditorGraph
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using UiInternalControls.WindowsFormsControls;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

internal class CUiJVLEditorGraph : TxGraphControl
{
  private CApJVLEditorViewModel _viewModel;
  private IContainer components;

  public CUiJVLEditorGraph()
  {
    this.InitializeComponent();
    this.registerToGraphEvents();
  }

  public CUiJVLEditorGraph(IContainer container)
  {
    container.Add((IComponent) this);
    this.InitializeComponent();
    this.registerToGraphEvents();
  }

  internal CApJVLEditorViewModel ViewModel
  {
    get => this._viewModel;
    set
    {
      if (this._viewModel == value)
        return;
      this.disconnectFromViewModel();
      this._viewModel = value;
      this.connectToViewModel();
    }
  }

  private void connectToViewModel()
  {
    if (this._viewModel == null)
      return;
    this._viewModel.Connect((Control) this);
    this.registerToViewModelEvents();
  }

  private void disconnectFromViewModel()
  {
    if (this._viewModel == null)
      return;
    this._viewModel.Disconnect((Control) this);
    this.unregisterToViewModelEvents();
  }

  private void registerToViewModelEvents()
  {
    if (this._viewModel == null)
      return;
    this._viewModel.UpdateStarted += new EventHandler(this._viewModel_UpdateStarted);
    this._viewModel.UpdateEnded += new EventHandler(this._viewModel_UpdateEnded);
  }

  private void unregisterToViewModelEvents()
  {
    if (this._viewModel == null)
      return;
    this._viewModel.UpdateStarted -= new EventHandler(this._viewModel_UpdateStarted);
    this._viewModel.UpdateEnded -= new EventHandler(this._viewModel_UpdateEnded);
  }

  private void registerToGraphEvents()
  {
    this.DataItemClicked += new TxGraph_DataItemClickedEventHandler(this.CUiJVLEditorGraph_DataItemClicked);
  }

  private void unregisterToGraphEvents()
  {
    this.DataItemClicked -= new TxGraph_DataItemClickedEventHandler(this.CUiJVLEditorGraph_DataItemClicked);
  }

  private void _viewModel_UpdateStarted(object sender, EventArgs e) => this.BeginUpdate();

  private void _viewModel_UpdateEnded(object sender, EventArgs e) => this.EndUpdate();

  private void CUiJVLEditorGraph_DataItemClicked(
    object sender,
    TxGraph_DataItemClickedEventArgs args)
  {
    if (!this._viewModel.ValidConnectedControls || !(this._viewModel[args.ItemIndex] is ITxGraphSelectablePoint graphSelectablePoint))
      return;
    List<ITxGraphPoint> itxGraphPointList = new List<ITxGraphPoint>(1);
    itxGraphPointList.Add((ITxGraphPoint) graphSelectablePoint);
    this._viewModel.BeginUpdate();
    this._viewModel.Selection = (IEnumerable<ITxGraphPoint>) itxGraphPointList;
    this._viewModel.FocusItem = (ITxGraphPoint) graphSelectablePoint;
    this._viewModel.EndUpdate();
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiJVLEditorGraph));
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this, "$this");
    ((Control) this).Name = nameof (CUiJVLEditorGraph);
    ((Control) this).ResumeLayout(false);
  }
}
